//
//  RepliedTableViewController.m
//  ChooseGift
//
//  Created by lanou on 15/12/19.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "RepliedTableViewController.h"
@interface RepliedTableViewController ()

@property (nonatomic,strong) NSMutableArray *contentArray;

@property (nonatomic,strong) NSMutableArray *imageArray;

@property (nonatomic,strong) NSMutableArray *nicknameArray;


@property (nonatomic,strong) NSMutableArray *contentArray1;

@property (nonatomic,strong) NSMutableArray *imageArray1;

@property (nonatomic,strong) NSMutableArray *nicknameArray1;

@end

@implementation RepliedTableViewController
//http://api.liwushuo.com/v2/items/1006220/comments?limit=20&offset=0


- (void)setData{
    self.contentArray = [NSMutableArray array];
    self.imageArray = [NSMutableArray array];
    self.nicknameArray = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/items/%@/comments?limit=20&offset=0",self.ID];
    NSURL *url = [NSURL URLWithString:string];
    NSLog(@"%@",string);
    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary * rootvc = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        NSArray *array = rootvc[@"data"][@"comments"];
        
        
        if (array.count == 0 ) {
            
            NSString *title = NSLocalizedString(@"提示", nil);
            NSString *message = NSLocalizedString(@"此物品没有评论", nil);
            NSString *cancelButtonTitle = NSLocalizedString(@"确定", nil);
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
            
            // Create the actions.
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                [self.navigationController popToRootViewControllerAnimated:YES];
                
                
            }];
            
            // Add the actions.
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
            
            
        }else{
            for (NSDictionary *dic in array) {
                if (dic[@"content"] == nil) {
                }else{
                    [self.contentArray addObject:dic[@"content"]];
                    
                    [self.imageArray addObject:dic[@"user"][@"avatar_url"]];
                    
                    [self.nicknameArray addObject:dic[@"user"][@"nickname"]];
                }
            }
            for (NSDictionary *dic in array) {
                if (dic[@"replied_comment"][@"content"] == nil) {
                    
                }else{
                    [self.contentArray addObject:dic[@"replied_comment"][@"content"]];
                    
                    [self.imageArray addObject:dic[@"replied_user"][@"avatar_url"]];
                    
                    [self.nicknameArray addObject:dic[@"replied_user"][@"nickname"]];
                    
                }
            }
        }
        
        [self.tableView reloadData];
        
    }];
}




- (void)viewDidLoad {
    [super viewDidLoad];
    [self setData];
    self.tableView.rowHeight = 60;
    self.title = @"评论";
   
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      NSURL *url = [NSURL URLWithString:self.imageArray[indexPath.row]];
    NSData *data = [[NSData alloc]initWithContentsOfURL:url];
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == 0) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    if (data == nil) {
        cell.imageView.image = [UIImage imageNamed:@"1195968.gif"];
    }else{
    cell.imageView.image = [UIImage imageWithData:data];
    }
    cell.imageView.layer.cornerRadius = 30;
    cell.imageView.layer.masksToBounds = YES;

    cell.textLabel.text = self.nicknameArray[indexPath.row];
    cell.detailTextLabel.text = self.contentArray[indexPath.row];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    
    
    
    return cell;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 60;
//}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
